<template>
  <div class="list">
    <!-- 头部 -->
    <div class="page-nav-bar">
      <van-nav-bar
        class="nav-bar"
        left-arrow
        fixed
        @click-left="$router.back()"
      >
        <template #title>
          <div class="location" @click="$router.push('/city')">
            <span>{{ myCity.label }}</span>
            <i class="iconfont icon-arrow"></i>
          </div>
          <div class="form">
            <i slot="icon" class="iconfont icon-seach"></i>
            <span class="text">请输入小区或地址</span>
          </div>
        </template>
        <template #right>
          <i class="iconfont icon-map" @click="$router.push('/map')"></i>
        </template>
      </van-nav-bar>
    </div>
    <div class="list-main">
      <!-- 选项区域 -->
      <van-dropdown-menu class="dropdown-menu" duration="0.00001">
        <van-dropdown-item ref="itemArea">
          <template #title>
            <div class="filter-root">
              <div class="filter-area">
                <span class="text">区域</span>
                <i class="iconfont icon-arrow"></i>
              </div>
            </div>
          </template>
          <van-picker
            show-toolbar
            :columns="areaColums"
            :loading="loading"
            toolbar-position="bottom"
            confirm-button-text="确定"
            @confirm="onConfirmArea"
            @cancel="onCancelArea"
          />
        </van-dropdown-item>
        <van-dropdown-item ref="itemRentType">
          <template #title>
            <div class="filter-root">
              <div class="filter-area">
                <span class="text">方式</span>
                <i class="iconfont icon-arrow"></i>
              </div>
            </div>
          </template>
          <van-picker
            show-toolbar
            :columns="conditions.rentType"
            :loading="loading"
            toolbar-position="bottom"
            confirm-button-text="确定"
            @confirm="onConfirmRentType"
            @cancel="onCancelRentType"
          />
        </van-dropdown-item>
        <van-dropdown-item ref="itemPrice">
          <template #title>
            <div class="filter-root">
              <div class="filter-area">
                <span class="text">租金</span>
                <i class="iconfont icon-arrow"></i>
              </div>
            </div>
          </template>
          <van-picker
            show-toolbar
            :columns="conditions.price"
            :loading="loading"
            toolbar-position="bottom"
            confirm-button-text="确定"
            @confirm="onConfirmPrice"
            @cancel="onCancelPrice"
          />
        </van-dropdown-item>
        <van-dropdown-item>
          <template #title>
            <div class="filter-root">
              <div class="filter-area" @click="isShow = true">
                <span class="text">筛选</span>
                <i class="iconfont icon-arrow"></i>
              </div>
            </div>
          </template>
        </van-dropdown-item>
      </van-dropdown-menu>
      <!-- 房屋信息 -->
      <div class="house">
        <house-item
          v-for="item in searchList"
          :key="item.houseCode"
          :obj="item"
        ></house-item>
        <div v-if="!isSearchListShow" class="empty-container">
          <van-image
            :src="require('../../assets/images/not-found.png')"
          ></van-image>
          <p class="rent-p">暂无房源</p>
        </div>
      </div>
    </div>
    <!-- 弹层 -->
    <van-popup v-model="isShow" position="right" class="filter-popup">
      <filter-popup-content
        :conditions="conditions"
        @roomType="roomType = $event"
        @oriented="oriented = $event"
        @floor="floor = $event"
        @characteristic="characteristic = $event"
      ></filter-popup-content>
      <footer class="footer">
        <van-button class="cancel" @click="cannelMore">取消</van-button>
        <van-button class="submit" @click="submitMore">提交</van-button>
      </footer>
    </van-popup>
  </div>
</template>

<script>
import { housesConditionAPI, housesAPI } from '@/api'
import HouseItem from '@/components/HouseItem.vue'
import { mapState } from 'vuex'
import FilterPopupContent from './components/FilterPopupContent.vue'
export default {
  name: '',
  components: { HouseItem, FilterPopupContent },
  props: {},
  data() {
    return {
      conditions: {},
      areaColums: [],
      loading: true,
      isShow: false,
      searchList: [],
      isSearchListShow: false,
      myArea: null,
      mySubway: null,
      myRentType: null,
      myPrice: null,
      roomType: [],
      oriented: [],
      floor: [],
      characteristic: []
    }
  },
  computed: {
    ...mapState(['myCity', 'RentType'])
  },
  watch: {},
  created() {
    this.housesCondition()
    this.houseSearch()
  },
  methods: {
    // 获取房屋查询条件
    async housesCondition() {
      const { body } = await housesConditionAPI(this.myCity.value)
      this.labelText(body, this.conditions)
      this.areaColums = [this.conditions.area, this.conditions.subway]
      this.areaColums.forEach((item) => {
        item.children.forEach((obj) => {
          obj.children = obj.children
            ? obj.children
            : [{ text: '', value: null }]
        })
      })
      this.loading = false
    },
    // 根据条件查询房屋
    async houseSearch() {
      const { body } = await housesAPI({
        cityId: this.myCity.value,
        area: this.myArea,
        subway: this.mySubway,
        rentType: this.RentType ? this.RentType : this.myRentType,
        price: this.myPrice,
        more: this.myMore,
        start: 1,
        end: 20
      })
      this.searchList = body.list
      this.isSearchListShow = body.count
      this.$store.commit('SET_RENT_TYPE', null)
    },
    // 递归，将label全部转换成text
    labelText(label, columns) {
      for (const k in label) {
        if (Object.prototype.toString.call(label[k]) === '[object Array]') {
          columns[k] = []
          this.labelText(label[k], columns[k])
        } else if (
          Object.prototype.toString.call(label[k]) === '[object Object]'
        ) {
          columns[k] = {}
          this.labelText(label[k], columns[k])
        } else {
          k === 'label' ? (columns['text'] = label[k]) : (columns[k] = label[k])
        }
      }
    },
    // 区域和地铁选择
    onConfirmArea(value, index) {
      if (index[0] === 0) {
        if (index[1] === 0 || index[2] === 0) {
          this.myArea = this.areaColums[index[0]].children[index[1]].value
        } else {
          this.myArea =
            this.areaColums[index[0]].children[index[1]].children[
              index[2]
            ].value
        }
        this.mySubway = null
      } else {
        if (index[1] === 0 || index[2] === 0) {
          this.mySubway = this.areaColums[index[0]].children[index[1]].value
        } else {
          this.mySubway =
            this.areaColums[index[0]].children[index[1]].children[
              index[2]
            ].value
        }
        this.myArea = null
      }
      this.houseSearch()
      this.$refs.itemArea.toggle()
    },
    // 关闭区域
    onCancelArea() {
      this.$refs.itemArea.toggle()
    },
    // 租房方式选择
    onConfirmRentType(value, index) {
      this.myRentType = this.conditions.rentType[index].value
      this.houseSearch()
      this.$refs.itemRentType.toggle()
    },
    // 关闭方式
    onCancelRentType() {
      this.$refs.itemRentType.toggle()
    },
    // 价格选择
    onConfirmPrice(value, index) {
      this.myPrice = this.conditions.price[index].value
      this.houseSearch()
      this.$refs.itemPrice.toggle()
    },
    // 关闭价格
    onCancelPrice() {
      this.$refs.itemPrice.toggle()
    },
    // 清除筛选
    cannelMore() {
      this.roomType = []
      this.oriented = []
      this.floor = []
      this.characteristic = []
    },
    // 筛选处理
    submitMore() {
      this.myMore = `${this.roomType.join(
        ','
      )},$this.oriented.join(',')},${this.floor.join(
        ','
      )},${this.characteristic.join(',')}`
      console.log(this.myMore)
      this.houseSearch()
      this.isShow = false
    }
  }
}
</script>

<style scoped lang="less">
.page-nav-bar {
  height: 50px;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  :deep(.nav-bar) {
    background-color: #21b97a;
    height: 50px;
    .van-nav-bar__content {
      height: 50px;
      .van-nav-bar__left {
        padding: 0 7px;
        .van-icon-arrow-left {
          color: #fff;
          font-size: 22px;
          margin: unset;
        }
      }
      .van-nav-bar__right {
        padding: 0 20px 0 0;
        .icon-map {
          font-size: 25px;
          color: #fff;
        }
      }
      .van-nav-bar__title {
        max-width: unset;
        margin: 0 0 0 46px;
        width: 274px;
        height: 34px;
        border-radius: 3px;
        display: flex;
        align-items: center;
        padding: 5px 5px 5px 8px;
        background-color: #fff;
        .location {
          width: 42px;
          font-size: 14px;
          color: #333;
          .icon-arrow {
            margin-left: 2px;
            font-size: 12px;
            color: #7f7f80;
          }
        }
        .form {
          border-left: 1px solid #e5e5e5;
          margin-left: 12px;
          .icon-seach {
            vertical-align: middle;
            padding: 0 2px 0 12px;
            color: #9c9fa1;
            font-size: 15px;
          }
          .text {
            padding-left: 4px;
            font-size: 13px;
            color: #9c9fa1;
          }
        }
      }
    }
  }
}
:deep(.filter-root) {
  display: flex;
  height: 40px;
  z-index: 50;
  border-bottom: 1px solid #e4e6f0;
  background-color: #fff;
  .filter-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    .text {
      font-size: 17px;
      color: #333;
    }
    .iconfont {
      margin-left: 3px;
      font-size: 12px;
      color: #bbb;
    }
  }
}
.house {
  z-index: 30;
  position: absolute;
  top: 40px;
  height: calc(100vh - 40px - 50px);
  overflow: auto;
  width: 100%;
  text-align: center;
  background-color: #fff;
  padding: 0 15px;
}
:deep(.van-picker) {
  .van-picker__columns {
    height: 238px !important;
    line-height: 1;
    .van-picker-column {
      margin-top: -10px;
    }
  }
  .van-picker__frame {
    height: 40px !important;
    &::after {
      border-color: #ddd;
    }
  }
  .van-picker__toolbar {
    height: 50px;
    margin-top: 20px;
    border-top: 1px solid #e9e9e9;
    .van-picker__cancel {
      flex: 1;
      font-size: 18px;
      color: #21b97a;
    }
    .van-picker__confirm {
      flex: 2;
      font-size: 18px;
      color: #fff;
      background-color: #21b97a;
    }
  }
  .van-picker-column__item {
    color: #333;
    font-size: 16px;
  }
  .van-picker-column__item--selected {
    color: rgb(16, 2, 2);
    font-size: 17px;
  }
}
:deep(.filter-popup) {
  height: 100%;
  width: calc(100vw - 80px);
  .footer {
    display: flex;
    height: 50px;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 20;
    margin-top: 20px;
    border-top: 1px solid #e9e9e9;
    .van-button {
      height: 50px;
    }
    .cancel {
      font-size: 15px;
      border: unset;
      flex: 1;
      color: #21b97a;
    }
    .submit {
      flex: 2;
      font-size: 15px;
      border: unset;
      color: #fff;
      background-color: #21b97a;
    }
  }
}
:deep(.dropdown-menu) {
  height: 40px;
  position: sticky;
  top: 0;
  z-index: 800;
  .van-dropdown-menu__bar {
    height: 40px;
  }
  .van-dropdown-menu__title {
    width: 100%;
    padding: unset;
    &::after {
      content: unset;
    }
  }
}
.van-dropdown-menu__title--active {
  .text {
    color: #21b97a !important;
  }
  .iconfont {
    color: #21b97a !important;
  }
}
.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  .van-image {
    width: 150px;
    height: 97.5px;
    margin: 30px 0;
  }
  .rent-p {
    font-size: 14px;
    color: #333;
  }
}
.list-main {
  position: sticky;
  top: 0;
  left: 0;
  z-index: 80;
  margin-top: 50px;
  height: calc(100vh - 50px);
  overflow: auto;
}
</style>
